package com.fbinggun.fbinggunutil.price;


import com.fbinggun.fbinggunutil.API.ItemUtil;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

/**
 * 物品价格
 */
public class ItemPrice implements IPrice{
   Map<String,Item> itemMap=new HashMap<>();


    /**
     * Price:
     *  PriceType: Item
     *  Item:
     *   id1:
     *    Type: STONE
     *    Count: 10
     *   id2:
     *    Type: SAND
     *    Count: 10
     * @param config
     */
    public ItemPrice(ConfigurationSection config) {
        if(config.isString("Item")){
            String itemType=config.getString("Item");
            int count=config.getInt("Count");
            Item item=new Item(itemType,count);
            itemMap.put("id1",item);
        }else {
            ConfigurationSection itemConfig = config.getConfigurationSection("Item");
            if(itemConfig!=null){
                for(String id:itemConfig.getKeys(false)){
                    Item item = new Item(itemConfig.getConfigurationSection(id));
                    itemMap.put(id,item);
                }
            }
        }

    }


    @Override
    public PriceType getType() {
        return PriceType.Item;
    }

    @Override
    public boolean has(Player player) {
        if(itemMap.size()!=0){
            for(Item item:itemMap.values()){
                if(!item.has(player)){
                    return false;
                }
            }
        }
       return true;
    }

    @Override
    public boolean take(Player player) {
        if(itemMap.size()!=0){
            for(Item item:itemMap.values()){
                item.take(player);
            }
            return true;
        }
        return true;
    }
}
class Item{
    String id;
    int count;
    public Item(ConfigurationSection configurationSection) {
        id=configurationSection.getString("Type");
        count=configurationSection.getInt("Count");
    }


    public Item(String id, int count) {
        this.id = id;
        this.count = count;
    }
    public boolean has(Player player) {
        return ItemUtil.isInvItemAmount(player,id,count);
    }
    public boolean take(Player player) {
        ItemUtil.takeInvItem(player,id,count);
        return false;
    }
}